package com.atguigu.eduservice.controller;


import com.atguigu.baseservice.handler.exception.GuliException;
import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.baseservice.result.ResultCodeInfo;
import com.atguigu.baseservice.result.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.query.EduTeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author dayouzi
 * @since 2022-08-30
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin //跨域
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/list")
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        teacherService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(name = "searchObj", value = "查询对象", required = false)EduTeacherQuery eduTeacherQuery){
        //新建分页参数
        Page<EduTeacher> pageParam = new Page<>(page, limit);

        //调用查询
        teacherService.pageQuery(pageParam, eduTeacherQuery);

        //获取结果
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/save")
    public R save(@ApiParam(name = "eduTeacher", value = "讲师对象", required = true) @RequestBody EduTeacher eduTeacher){
        teacherService.save(eduTeacher);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("item", eduTeacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id,
                        @ApiParam(name = "eduTeacher", value = "讲师对象", required = true) @RequestBody EduTeacher eduTeacher){
        eduTeacher.setId(id);
        teacherService.updateById(eduTeacher);
        return R.ok();
    }

    @GetMapping("/test")
    public void test(){
        try {
            int i = 1/0;
        } catch (Exception e){
            throw new GuliException(ResultCodeEnum.INTERNAL_SERVER_ERROR);
        }

    }

}

